
interface Compose { 
    name: string,
    add(file: CFile): void,
    scan(): void
}

/**
 * 树对象-文件目录
 */

class CFolder implements Compose { 
    fileList = []
    name: string

    constructor(name: string) { 
        this.name = name
    }

    add(file: CFile) { 
        this.fileList.push(file)
    }

    scan() { 
        for (const file of this.fileList) {
            file.scan()
        }
    }
}

class CFile implements Compose { 
    name: string;

    constructor(name) { 
        this.name = name
    }

    add(file: CFile) { 
        throw Error("文件下面不能再添加文件")
    }

    scan() { 
        console.log(`扫描到文件${this.name}`)
    }
}

let mediaFolder = new CFolder("娱乐")

let movieFolder = new CFolder("电影")
let musicFolder = new CFolder("音乐")

let file1 = new CFile("钢铁侠.mp4")
let file2 = new CFile("在谈记忆.mp3")
movieFolder.add(file1)
movieFolder.add(file2)
mediaFolder.add(movieFolder)
mediaFolder.add(musicFolder)
mediaFolder.scan()